var api = require('../api/chongdian/index.js');
import store from '@/store'
import {
	getToken,
	setToken,
	removeToken
} from '@/utils/auth'
module.exports = {
	formatTime,
	request,
	requestKit,
	redirect,
	showSuccessToast,
	showErrorToast,
	checkSession,
	login,
	getUserInfo,
	getQueryString,
	formatDate,
	getLocationFun,
	formatBarcode
};

function formatTime(date) {
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var hour = date.getHours();
	var minute = date.getMinutes();
	var second = date.getSeconds();
	return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':');
}

function formatBarcode(params) {
	if (params == '' || !params) {
		return '';
	}
	let code = '';
	// params = 'hlht://3702030189110.395815801/'
	if (params.indexOf('/') !== -1) {
		let split = params.split('/');
		code = split[2].split('.')[0];
	} else {
		code = params;
	}
	console.log(code);
	return code;
}

function formatDate(date) {
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	return [year, month, day].map(formatNumber).join('-');
}

function formatNumber(n) {
	n = n.toString();
	return n[1] ? n : '0' + n;
}
/**
 * 封封微信的的request  'POST', 'application/json'
 * "POST","application/x-www-form-urlencoded"
 * method = "POST", header = "application/x-www-form-urlencoded"
 */
function requestKit(url, data = {}, method = 'POST', header = 'application/json') {
	return new Promise(function(resolve, reject) {
		uni.request({
			url: url,
			data: data,
			method: method,
			header: {
				'Content-Type': header,
				token: getToken()
			},
			success: function(res) {
				uni.hideLoading();
				if (res.statusCode == 200) {
					resolve(res.data);
				} else {
					reject(res.errMsg);
				}
			},
			fail: function(err) {
				uni.hideLoading();
				console.log('-------url----err----', err);
				reject(err);
			}
		});
	});
}
/**
 * 封封微信的的request
 */
function request(url, data, method) {
	return new Promise(function(resolve, reject) {
		uni.request({
			url: url,
			data: data,
			method: method,
			header: {
				'Content-Type': 'application/x-www-form-urlencoded',
				token: getToken()
			},
			success: function(res) {
				resolve(res.data);
			},
			complete: function(e) {
				// console.log('e', e);
				if (e.data.code == 102) {

					// store.dispatch('user/LogOut').then(res => {
					// uni.reLaunch({
					// 	url: '/pages/mine/index'
					// })
					// console.log('clear');
					// })
				}

			},
			fail: function(e) {
				reject(e);
				console.log('微信网络请求异常', e);
			}
		});
	});
}

function getLocationFun(func, errFunc) {
	//wx.getFuzzyLocation  uni.getLocation
	uni.getLocation({
		type: 'wgs84',
		success(res) {
			func(res);
		},
		fail: function(err) {
			errFunc(err);
		}
	});
}
/**
 * 检查微信会话是否过期
 */
function checkSession() {
	return new Promise(function(resolve, reject) {
		uni.checkSession({
			success: function() {
				resolve(true);
			},
			fail: function() {
				reject(false);
			}
		});
	});
}

/**
 * 调用微信登录
 */
function login() {
	return new Promise(function(resolve, reject) {
		uni.login({
			success: function(res) {
				if (res.code) {
					//登录远程服务器
					console.log(res);
					resolve(res);
				} else {
					reject(res);
				}
			},
			fail: function(err) {
				reject(err);
			}
		});
	});
}

function getUserInfo() {
	return new Promise(function(resolve, reject) {
		uni.getUserInfo({
			withCredentials: true,
			success: function(res) {
				console.log(res);
				resolve(res);
			},
			fail: function(err) {
				reject(err);
			}
		});
	});
}

function redirect(url) {
	//判断页面是否需要登录

	uni.redirectTo({
		url: url
	});
}

function showSuccessToast(msg) {
	uni.showToast({
		title: msg,
		icon: 'none',
		duration: 1500
	});
}

function showErrorToast(msg) {
	uni.showToast({
		// type: "error",
		title: msg,
		icon: 'none',
		duration: 1500
	});
}

function getQueryString(query, name) {
	var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
	var r = query.match(reg);
	if (r != null) {
		return unescape(r[2]);
	}
	return null;
}